/*
 * N900DevStatus.cpp
 *
 *  Created on: Aug 22, 2016
 *      Author: ubuntu
 */

#include "N900DevStatus.h"
#include "../common.h"
#include <mysql++/mysql++.h>
#include "../Conf.h"

using namespace mysqlpp;

N900DevStatus::N900DevStatus(unsigned int obdid)
{
	m_obdid = obdid;
	m_time = time(NULL);
	check_time = 0;
	car_time = 0;
	m_version = 0;
	m_update = false;
	m_speed = 0;
	max_time = 0;
}

N900DevStatus::~N900DevStatus()
{
}

unsigned int N900DevStatus::getObdid()
{
	return m_obdid;
}

time_t N900DevStatus::getTime()
{
	return m_time;
}

time_t N900DevStatus::getMaxTime()
{
	return max_time;
}

time_t N900DevStatus::getCheckTime()
{
	return check_time;
}

time_t N900DevStatus::getCarTime()
{
	return car_time;
}

void N900DevStatus::enableUpdate()
{
	m_update = true;
}

void N900DevStatus::disableUpdate()
{
	m_update = false;
}

bool N900DevStatus::isUpdate()
{
	Conf *conf = getConf();
	try {
		Connection conn(conf->dbname, conf->dbhost, conf->dbuser, conf->dbpass, conf->dbport);
		Query query = conn.query();
		query << "select updateable from zb_n900_allobd where obdid=" << m_obdid;
		StoreQueryResult res = query.store();
		if (res.num_rows() > 0)
			m_update = atoi(res[0][0].c_str()) == 1 ? true : false;
		conn.disconnect();
	} catch (mysqlpp::BadQuery &er) {
	} catch (mysqlpp::Exception &er) {
	} catch (...) {
	}
	return m_update;
}

void N900DevStatus::setTime(time_t ts, int type)
{
	switch (type) {
	case N900_CHECK_TIME:
		if (ts > time(NULL)) {
			check_time = time(NULL);
		} else {
			check_time = ts;
		}
		break;
	case N900_CAR_TIME:
		if (ts > time(NULL)) {
			car_time = time(NULL);
		} else {
			car_time = ts;
		}
		break;
	case N900_MAX_TIME:
		if (ts > max_time && ts <= time(NULL)) {
			max_time = ts;
		}
		break;
	default:
		break;
	}
}

void N900DevStatus::setSpeed(int speed)
{
	if (speed >= 0)
		m_speed = speed;
}

int N900DevStatus::getSpeed()
{
	return m_speed;
}

